package com.example.qixinshangwutong.view.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.bumptech.glide.Glide;
import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.view.fragment.BrandFragment;
import com.example.qixinshangwutong.view.fragment.FollowFragment;
import com.example.qixinshangwutong.view.fragment.HomeFragment;
import com.example.qixinshangwutong.view.fragment.MyFragment;
import com.example.qixinshangwutong.view.fragment.NewsFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import cn.leancloud.LCUser;

public class BottomActivity extends AppCompatActivity {
    private List<Fragment> fragmentList=new ArrayList<>();

    private BottomNavigationView bnv;
    private HomeFragment fragment;
    private int itemId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom);
        initView();
        click();

    }
    private void initView(){
        bnv=findViewById(R.id.bottom);
        fragment=new HomeFragment();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new BrandFragment());
        fragmentList.add(new NewsFragment());
        fragmentList.add(new FollowFragment());
        fragmentList.add(new MyFragment());
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            ft.add(R.id.fragment_contain,fragmentList.get(i));
            ft.hide(fragmentList.get(i));
            if (i==0){
                ft.show(fragmentList.get(0));
            }
        }
        ft.commit();
        bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
                itemId = item.getItemId();
                showSelectFragment(itemId);
                return true;
            }
        });


    }
    private void showSelectFragment(int menuId){
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        for (int i = 0; i <fragmentList.size() ; i++) {
            ft.hide(fragmentList.get(i));
            switch (menuId){
                case R.id.home_item_home:
                    ft.show(fragmentList.get(0));
                    break;

                case R.id.home_item_brand:
                    ft.show(fragmentList.get(1));
                    break;

                case R.id.home_item_news:
                    ft.show(fragmentList.get(2));
                    break;

                case R.id.home_item_follow: {
                    LCUser currentUser = LCUser.getCurrentUser();
                    if (currentUser != null) {
                        ft.show(fragmentList.get(3));
                        break;
                    } else {
                        Intent intent = new Intent(BottomActivity.this, LoginActivity.class);
                        startActivity(intent);
                        break;
                    }
                }

                case R.id.home_item_me:
                    ft.show(fragmentList.get(4));
                    break;

            }
        }
        ft.commit();
    }
    private void click(){
        fragment.setListener(new HomeFragment.onMoreClickListener() {
            @Override
            public void onItemClick() {
                FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
                BrandFragment brandFragment=new BrandFragment();
                ft.addToBackStack(null).replace(R.id.home_item_home,brandFragment);
                ft.commit();


                //bnv.setSelectedItemId(R.id.home_item_brand);

            }

            @Override
            public void onNewsClick() {
                bnv.setSelectedItemId(R.id.home_item_news);

            }
        });
    }
}